package com.zmn.oms.api.controller.third.dj58;

import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.utils.RequestUtil;
import com.zmn.oms.third.dj58.DaoJia58Consts;
import com.zmn.oms.third.dj58.dto.DaoJia58SyncOrderStateDTO;
import com.zmn.oms.third.dj58.dto.api.DaoJia58Response;
import com.zmn.oms.third.dj58.service.DaoJia58Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 类描述：58到家对外接口
 *  用于58到家直约服务向我方的订单推送及订单状态变更推送
 *
 * @author XieWenBing
 * @date 2019/11/01 11:00
 */
@RestController
@RequestMapping("/daoJia58")
@Slf4j
public class DaoJia58Controller {
    @Autowired
    private DaoJia58Service dj58Service;

    /**
     * 接收58到家推送的订单消息（创建线索，补充支付，取消订单，订单完成，订单金额变更等）
     */
    @PostMapping("/agreement/syncOrderMsg")
    @ResponseBody
    public DaoJia58Response syncOrderMsg(HttpServletRequest request) {
        String paramsStr = RequestUtil.getPostParameter(request);
        log.info("【{}】收到订单消息！ Body:[{}]", DaoJia58Consts.CHANNEL_NAME, paramsStr);

        return dj58Service.onMessage(paramsStr);
    }

    /**
     * 人工同步订单状态给58
     */
    @PostMapping(value="/oms/syncOrderState")
    @ResponseBody
    public ResultDTO syncOrderState(@NotNull @RequestBody DaoJia58SyncOrderStateDTO syncOrderStateDTO) {

        try {
            return dj58Service.syncOrderState(syncOrderStateDTO);
        } catch (OmsBaseException omsBaseException) {
            return ResultDTO.fail(Objects.nonNull(omsBaseException)?omsBaseException.getMessage():"异常");
        }
    }
}
